package inits

import (
	"GoMaestro/internal/logger"
	"GoMaestro/internal/router"
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func R(r gin.IRouter) {
	r.Use(logger.GinLoggerMiddleware(), InitCORS())
	// 基础路由
	r1 := router.AppGroup.JenkinsRouter
	r1.Router(r)
	// 发布路由
	r2 := router.AppGroup.PublishRouter
	r2.Router(r)
}

func InitCORS() gin.HandlerFunc {
	return cors.New(cors.Config{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, // 添加 OPTIONS
		AllowHeaders: []string{
			"Origin",
			"Authorization",
			"Content-Type",
			"Accept",
			"X-Requested-With",
			"Access-Control-Allow-Methods",
		},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	})
}

// AuthMiddleware 验证密钥中间件
//func AuthMiddleware() gin.HandlerFunc {
//	return func(c *gin.Context) {
//		// 处理 OPTIONS 请求
//		if c.Request.Method == "OPTIONS" {
//			c.Next()
//			return
//		}
//
//		// 验证密钥
//		if config.CONF.System.Serct != c.Request.Header.Get("Authorization") {
//			logger.DefaultLogger.Error(fmt.Errorf("配置秘钥认证失败！"))
//			responses.ResponseApp.FailWithAgent(c, "", "配置秘钥认证失败！")
//			c.Abort()
//			return
//		}
//		c.Next()
//	}
//}
